from rest_framework import serializers

from Headlines.models import Channel, Article, Comment
from question.models import Label
from users.models import User


#频道序列化器
# from users.serializers import CreateUserSerializer


class ChannelModelSerializer(serializers.ModelSerializer):
    class Meta:
        model=Channel
        fields=['id','name']

class ArticlesimpleSerializer(serializers.ModelSerializer):
    class Meta:
        model=Article
        fields=['id','title']
class UsersimpleSerializer(serializers.ModelSerializer):
    class Meta:
        model=User
        fields=['username']

class UserModelSerializer(serializers.ModelSerializer):
    articles=ArticlesimpleSerializer(many=True)
    fans=UsersimpleSerializer(many=True)
    class Meta:
        model=User
        fields=['id','username','avatar','articles','fans']
class ArticleModelSerializer(serializers.ModelSerializer):
    user=UserModelSerializer()
    collected_users= UsersimpleSerializer(many=True)
    class Meta:
        model=Article
        fields=['id','title','content','createtime','user','collected_users','image']
class CommentSerializer(serializers.ModelSerializer):
    user = UserModelSerializer()
    subs=serializers.StringRelatedField(many=True)
    article_id = serializers.IntegerField()
    parent_id = serializers.IntegerField(default=None),
    class Meta:
        model=Comment
        fields=['id','user','content','article','article_id','parent_id','parent','createtime','subs']

class CommentModelSerializer(serializers.ModelSerializer):
    user = UserModelSerializer()
    subs=CommentSerializer(many=True)
    parent_id=serializers.IntegerField(default=None)
    article_id=serializers.IntegerField()
    class Meta:
        model=Comment
        fields=['id','user','subs','content','createtime','updatetime','parent','parent_id','article','article_id']
class LabelModelSerializer(serializers.ModelSerializer):
    class Meta:
        model=Label
        fields=['id']

class ArticleModelSerializer1(serializers.ModelSerializer):
    user=UserModelSerializer()
    comments=CommentModelSerializer(many=True)
    collected_users= UsersimpleSerializer(many=True)
    labels=LabelModelSerializer(many=True)
    collected = serializers.BooleanField(default=False)
    class Meta:
        model=Article
        fields=['id','user','comments','title','content','image','createtime','updatetime',
                'visits','thumbup','comment_count','channel','collected','labels','collected_users']



class ArticleSearchModelSerializer(serializers.ModelSerializer):
    class Meta:
        model = Article
        fields = ['id','createtime','content','title']